/**
 * fileName:  FruitCategoryImpl.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-5-31 上午11:32:01
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-5-31          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.handler.support;

import java.util.List;

import org.cloudland.commercial.masterdata.bean.ChooseType;
import org.cloudland.commercial.masterdata.bean.FruitCategoryInfo;
import org.cloudland.commercial.masterdata.bean.ParamValueRelation;
import org.cloudland.commercial.masterdata.handler.FruitCategoryStore;
import org.cloudland.commercial.masterdata.store.entity.CategoryEntity;
import org.cloudland.commercial.masterdata.store.entity.ParamValueRelEntity;
import org.cloudland.commercial.masterdata.store.read.ArgumentCostReader;
import org.cloudland.commercial.masterdata.store.read.ArgumentReader;
import org.cloudland.commercial.masterdata.store.read.FruitCategoryReader;
import org.cloudland.commercial.masterdata.store.read.ParamValueRelReader;
import org.cloudland.commercial.masterdata.store.write.FruitCategoryWriter;
import org.cloudland.commercial.masterdata.store.write.ParamValueRelWriter;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  FruitCategoryImpl
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class FruitCategoryImpl implements FruitCategoryStore {
	
	/**
	 * fruitCategoryWriter 水果分类更新操作
	 */
	private FruitCategoryWriter fruitCategoryWriter;
	
	/**
	 * fruitCategoryReader 水果分类查询操作
	 */
	private FruitCategoryReader fruitCategoryReader;
	
	/**
	 * paramValueRelWriter 分类引用参数与值关系写操作
	 */
	private ParamValueRelWriter paramValueRelWriter;
	
	/**
	 * paramValueRelReader 分类引用参数与值关系读操作
	 */
	private ParamValueRelReader paramValueRelReader;
	
	/**
	 * argumentReader 参数读取操作类
	 */
	private ArgumentReader argumentReader;
	
	/**
	 * costReader 值读取操作类
	 */
	private ArgumentCostReader costReader;

	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public FruitCategoryImpl(FruitCategoryWriter fruitCategoryWriter, FruitCategoryReader fruitCategoryReader, ParamValueRelWriter paramValueRelWriter, ParamValueRelReader paramValueRelReader, ArgumentReader argumentReader, ArgumentCostReader costReader) {
		this.fruitCategoryWriter = fruitCategoryWriter;
		this.fruitCategoryReader = fruitCategoryReader;
		this.paramValueRelWriter = paramValueRelWriter;
		this.paramValueRelReader = paramValueRelReader;
		this.argumentReader = argumentReader;
		this.costReader = costReader;
	}

	/**
	 * @return
	 */
	public FruitCategoryInfo createCategory() {
		FruitCategoryInfo categoryInfo = new FruitCategoryBean(fruitCategoryWriter, fruitCategoryReader, paramValueRelReader, argumentReader, costReader);
		return categoryInfo;
	}
	
	/**
	 * @return
	 */
	public ParamValueRelation createParamValueRelation(String categoryId) {
		return new ParamValueRelBean(categoryId, paramValueRelWriter);
	}

	/**
	 * @param categoryId
	 * @return
	 */
	public FruitCategoryInfo findCategory(String categoryId) {
		CategoryEntity entity = fruitCategoryReader.findCategoryById(categoryId);
		FruitCategoryBean bean = new FruitCategoryBean(entity, fruitCategoryWriter, fruitCategoryReader, paramValueRelReader, argumentReader, costReader);
		
		return bean;
	}
	
	/**
	 * @param relationId
	 * @return
	 */
	public ParamValueRelation findParamValueRelationById(String relationId) {
		ParamValueRelEntity entity = paramValueRelReader.findParamValueRelById(relationId);
		return new ParamValueRelBean(entity, paramValueRelWriter);
	}
	
	/**
	 * @param categoryId
	 */
	public void remove(String categoryId) {
		CategoryEntity entity = fruitCategoryReader.findCategoryById(categoryId);
		if (null == entity || entity.getId().length() <= 0) {
			return;
		}
		fruitCategoryWriter.delete(entity);
	}
	
	/**
	 * @param relationId
	 */
	public void removeParamValueRelation(String relactionId) {
		paramValueRelWriter.delete(new ParamValueRelEntity(relactionId, "", "", "", ChooseType.CUSTOM, 1));
	}
	
	/**
	 * @return
	 */
	public FruitCategoryInfo[] findRootCategory() {
		List<CategoryEntity> entityArray = fruitCategoryReader.findCategoryRoot();
		if (null == entityArray) {
			return null;
		}
		
		FruitCategoryInfo[] resultArray = new FruitCategoryInfo[entityArray.size()];
		for (int i = 0; i < entityArray.size(); i++) {
			resultArray[i] = new FruitCategoryBean(entityArray.get(i), fruitCategoryWriter, fruitCategoryReader, paramValueRelReader, argumentReader, costReader);
		}
		
		return resultArray;
	}

}
